import { observable, action, runInAction } from 'mobx-miniprogram';
import projectRepository from '../services/project_repository';
// 使用统一配置入口引用模型
const ModelsConfig = require('../../config/models_config');
const { ProjectModel } = ModelsConfig;
import dayjs from 'dayjs';
import { cacheInitializer } from '../helpers/cache_initializer';
import { cacheDependencyManager } from '../helpers/cache_dependency_manager';

class ProjectStore {
    // 可观察状态
    @observable loading = false;
    @observable projects = [];
    @observable currentProject = null;
    @observable projectCount = 0;
    @observable filter = {
        status: 'all',
        keyword: ''
    };
    
    // 计算属性
    get filteredProjects() {
        return this.projects.filter(project => {
            // 状态筛选
            if (this.filter.status !== 'all' && project.status != this.filter.status) {
                return false;
            }
            
            // 关键词筛选
            if (this.filter.keyword && !project.name.includes(this.filter.keyword)) {
                return false;
            }
            
            return true;
        });
    }
    
    // 行为方法
    @action
    async loadProjects() {
        this.loading = true;
        try {
            const result = await projectRepository.getProjects();
            runInAction(() => {
                this.projects = result.data.map(item => this.formatProject(item));
                this.projectCount = result.data.length;
                this.loading = false;
                
                // 更新缓存
                cacheInitializer.updateCache('projects', this.projects);
                cacheDependencyManager.notifyUpdate('projects');
            });
        } catch (error) {
            runInAction(() => {
                this.loading = false;
            });
            console.error('加载项目失败', error);
        }
    }
    
    @action
    setFilter(filter) {
        this.filter = {
            ...this.filter,
            ...filter
        };
    }
    
    @action
    async loadProjectDetail(id) {
        this.loading = true;
        try {
            const detail = await projectRepository.getProjectDetail(id);
            runInAction(() => {
                this.currentProject = this.formatProject(detail);
                this.loading = false;
            });
        } catch (error) {
            runInAction(() => {
                this.loading = false;
            });
            console.error('加载项目详情失败', error);
        }
    }
    
    // 工具方法
    formatProject(project) {
        return {
            ...project,
            startDateFormatted: dayjs(project.startDate).format('YYYY-MM-DD'),
            endDateFormatted: dayjs(project.endDate).format('YYYY-MM-DD'),
            statusText: ProjectModel.getStatusText(project.status)
        };
    }
    
    // 清理方法
    @action
    reset() {
        this.projects = [];
        this.currentProject = null;
        this.loading = false;
        this.filter = {
            status: 'all',
            keyword: ''
        };
    }
}

export default new ProjectStore();